package com.hb.game.pk.ui.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.DoTxBean;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.event.MoneyChangeEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.mine.setting.account.VerifyCodeActivity;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.numbercodeview.BaseNumberCodeView;
import com.hb.game.pk.widgets.numbercodeview.bottomsheet.BottomSheetNumberCodeView;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WithdrawCashActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_yue)
    TextView tvYue;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.bsn_pwd)
    BottomSheetNumberCodeView bsnPwd;
    private MaterialDialog alertDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WithdrawCashActivity.this.finish();
            }
        });
        tvYue.setText(String.format(getResources().getString(R.string.activity_cash), "0"));
        bsnPwd.setIsPassword(true);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        Call<NullBean> getmoney = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getmoney();
        getmoney.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (body.getData() != null && !StringUtils.isEmpty(body.getData().toString()))
                    tvYue.setText(String.format(getResources().getString(R.string.activity_cash), (Float.parseFloat(body.getData().toString()) / 100) + ""));
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        if (!checkTime()) {
            ToastUtils.showToast("请在每天10点-22点之间提现");
            return;
        }
        String s = etMoney.getText().toString();
        if (StringUtils.isEmpty(s)) {
            ToastUtils.showToast("请输入提现金额");
            return;
        }
//        BottomSheetNumberCodeViewActivity.show(this, true);
        if (APP.bankInfo != null && StringUtils.isEmpty(APP.bankInfo.getBank_num())) {
            showBindDialog(1);
        } else if (APP.userInfo != null && StringUtils.isEmpty(APP.userInfo.getZfb_pwd())) {
            showBindDialog(2);
        } else {
            bsnPwd.setVisibility(View.VISIBLE);
            btnOk.setVisibility(View.GONE);
            bsnPwd.showNumberCodeLayout();
            bsnPwd.setOnHideBottomLayoutListener(new BottomSheetNumberCodeView.OnHideBottomLayoutListener() {
                @Override
                public void onHide() {
                    bsnPwd.setVisibility(View.GONE);
                    btnOk.setVisibility(View.VISIBLE);
                    hiddenKeyboard();
                }
            });
            bsnPwd.setNumberCodeCallback(new BaseNumberCodeView.OnInputNumberCodeCallback() {
                @Override
                public void onResult(String code) {
                    if (StringUtils.isEmpty(code))
                        return;
                    Call<DoTxBean> tx = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).tx(etMoney.getText().toString(), Integer.parseInt(code));
                    tx.enqueue(new Callback<DoTxBean>() {
                        @Override
                        public void onResponse(Call<DoTxBean> call, Response<DoTxBean> response) {
                            DoTxBean body = response.body();
                            if (body == null) {
                                ToastUtils.showToast("请求错误");
                                return;
                            }
                            if (body.getStatus() != 1) {
                                ToastUtils.showToast(body.getInfo());
                                return;
                            }
                            DoTxBean.DataBean data1 = body.getData();
                            ToastUtils.showToast(data1.getMsg());
                            if (data1.getStatus() == 1) {
                                EventBus.getDefault().post(new MoneyChangeEvent());
                                WithdrawCashActivity.this.finish();
                            }
                        }

                        @Override
                        public void onFailure(Call<DoTxBean> call, Throwable t) {
                            ToastUtils.showToast("网络请求错误");
                        }
                    });
                }
            });
        }
    }

    private void showBindDialog(final int type) {
        if (alertDialog == null) {
            View inflate = View.inflate(this, R.layout.dialog_alert, null);
            TextView tvCansle = (TextView) inflate.findViewById(R.id.tv_cansle);
            TextView tvTx = (TextView) inflate.findViewById(R.id.tv_tx);
            if (type == 1)
                tvTx.setText("未绑定银行卡，是否去添加?");
            else
                tvTx.setText("未设置支付密码，是否去设置?");
            TextView tvOk = (TextView) inflate.findViewById(R.id.tv_ok);
            tvCansle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                }
            });
            tvOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
//                        startActivity(BindActivity.class);
                    if (type == 1)
                        startActivity(BindCardActivity.class);
                    else
                        startActivity(VerifyCodeActivity.class);
                }
            });
            alertDialog = new MaterialDialog.Builder(this).customView(inflate, false).show();
        } else {
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            } else {
                alertDialog.show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post(new MoneyChangeEvent());
    }

    public boolean checkTime() {
        Calendar cal = Calendar.getInstance();// 当前日期
        int hour = cal.get(Calendar.HOUR_OF_DAY);// 获取小时
        int minute = cal.get(Calendar.MINUTE);// 获取分钟
        int minuteOfDay = hour * 60 + minute;// 从0:00分开是到目前为止的分钟数
        final int start = 10 * 60;// 起始时间 00:20的分钟数
        final int end = 22 * 60;// 结束时间 8:00的分钟数
        if (minuteOfDay >= start && minuteOfDay <= end) {
            return true;
        } else {
            return false;
        }
    }
}
